home *** CD-ROM | disk | FTP | other *** search
/ SPACE 2 / SPACE - Library 2 - Volume 1.iso / program / 322 / flex / mktemp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-20  |  902 b   |  46 lines

  1.  
  2. /* well, this is a complete crock of shit too. */
  3.  
  4. #include "stdio.h"
  5. #include "types.h"
  6. #include "stat.h"
  7.  
  8. char * mktemp(pattern)
  9. char * pattern;
  10. {
  11.   char template[64];
  12.   char result[64];
  13.   char * p, * q;
  14.   int tempnum;
  15.   struct stat ignored;
  16.  
  17. /* first copy the name in, searching for the "XXX..." */
  18.   for (p = pattern, q = (char * )&template ; *p ; p++)
  19.     {
  20.     if (*p == 'X')
  21.         break;
  22.     *q++ = *p;
  23.     }
  24.   *q++ = '%';
  25.   *q++ = 'd';
  26.   while (*p && (*p == 'X'))
  27.     p++;
  28. /* copy the rest */
  29.   for ( ; *p ; )
  30.     *q++ = *p++;
  31.   *q = '\0';
  32.  
  33. /* generate the name, but don't try forever if they gave us something
  34.    completely bogus. */
  35.   for (tempnum = 0 ; tempnum < 100 ; tempnum++)
  36.     {
  37.     sprintf(&result, &template, tempnum);
  38.       if (stat(&result, &ignored) != 0)
  39.         break;
  40.     }
  41.   if (tempnum >= 100)        /* lost */
  42.     return(0L);
  43.   strcpy(pattern, &result);
  44.   return(pattern);
  45. }
  46.